<?php
include_once CF_FUNCTIONS . "CF_support.php";

// This class will define basic query filter class funcitons
// It leaves out any database access specific ones
// it contains the methods and variables for getting the filters and intrepreting them

class CF_FilterDataPdo{


	/**
	* @var array $filters Holds fitlers as array filers[<column>] = <value> filled by get_filter_vals().  This expects filter variables form the $_REQUEST or from the $_SESSION if saved (pass empty to clear).  Uses prefix determined by $filterprefix
	*/
	var $filters; 


	function CF_FilterDataPdo(&$filters){
		__construct($filters);
	}
	
	function __construct(&$filters){
		$this->filters = $filters;	
	}

	
	function get_filterspec(){
		return $this->filters;
	}



	/**
	 * make_query_PSfilter will create a  prepare staement placeholder query filter according to fieldspec settings, Request input, and Session data
	 *
	 * $this->filters[filterparam][var] = column variable to filter
	 * $this->filters[filterparam][var] = current value
	 * $this->filters[filterparam][var] = operation of filter, to later use in query (=,<,>, etc)
	 */	 
	function make_query_PSfilter(){
		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filterparam => $filter){
				if(isset($filter['source'])){
					$tabledot = $filter['source'] . '.';
				}
				$flt_text .= " AND $tabledot" . $filter['var'] . ' ' . $filter['op'] . " :FLT" . $filter['var'] . " ";
				
			}	
		}
	
	return $flt_text;
	}
	
	
	function make_PSfilter_bindings(&$stmt){
			if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
                $currentvar = $filter['var'];				
					if(isset($filter['source'])){
							$tabledot = $filter['source'] . '.';
						}					
						$bindvar = ':FLT'. $filter['var'];
						$value = $filter['val'];
						if($filter['op'] == 'LIKE' AND $this->keyword_filter == true){
							$value = '%' . $value . '%';
							}							
						$stmt->bindValue($bindvar,$value);				
			}	
		}
	}
	

	
	function make_doctrine_filter(&$docquery, $objectname){
		if(!empty( $this->filters)) {
			foreach($this->filters AS $filterparam => $filterspec){
				//$newandwhere = $objectname . '.' . $filterspec['var'] . $filterspec['op'] .  ' ? ';
				//print $newandwhere;
				$value = $filterspec['val'];
				$docquery->andWhere($objectname . '.' . $filterspec['var'] . ' ' . $filterspec['op'] .  ' ? ', array($value));
			}
		}
		return $docquery;
	}

	
}


?>